package com.jhgsys.internal.accident.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.accident.entity.AccidentAlertsFile;
import com.jhgsys.internal.accident.mapper.AccidentAlertsFileMapper;
import com.jhgsys.internal.accident.service.IAccidentAlertsFileService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 安全警报附件 Service实现
 *
 * @author zhangwei
 * @date 2024-08-23 13:28:12
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentAlertsFileServiceImpl extends ServiceImpl<AccidentAlertsFileMapper, AccidentAlertsFile> implements IAccidentAlertsFileService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     * @param request QueryRequest
     * @param accidentAlertsFile accidentAlertsFile
     * @return IPage<AccidentAlertsFile>
    */
    @Override
    public IPage<AccidentAlertsFile> findAccidentAlertsFilePage(QueryRequest request, AccidentAlertsFile accidentAlertsFile) {
        LambdaQueryWrapper<AccidentAlertsFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentAlertsFile::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentAlertsFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     * @param accidentAlertsFile accidentAlertsFile
     * @return List<AccidentAlertsFile>
     */
    @Override
    public List<AccidentAlertsFile> findAccidentAlertsFileList(AccidentAlertsFile accidentAlertsFile) {
	    LambdaQueryWrapper<AccidentAlertsFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentAlertsFile::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-08-23 13:28:12
    * @param id
    * @return AccidentAlertsFile
    */
    @Override
    public AccidentAlertsFile findAccidentAlertsFileById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     * @param accidentAlertsFile accidentAlertsFile
     */
    @Override
    @Transactional
    public void createAccidentAlertsFile(AccidentAlertsFile accidentAlertsFile) {
        accidentAlertsFile.setCreatedTime(new Date());
        this.save(accidentAlertsFile);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     * @param accidentAlertsFile accidentAlertsFile
     */
    @Override
    @Transactional
    public void updateAccidentAlertsFile(AccidentAlertsFile accidentAlertsFile) {
        accidentAlertsFile.setUpdatedTime(new Date());
        this.saveOrUpdate(accidentAlertsFile);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-08-23 13:28:12
     * @param accidentAlertsFile accidentAlertsFile
     * @param accidentAlertsFileIds accidentAlertsFileIds
     */
    @Override
    @Transactional
    public void deleteAccidentAlertsFile(AccidentAlertsFile accidentAlertsFile,String accidentAlertsFileIds) {
        List<String> list = Arrays.asList(accidentAlertsFileIds.split(StringPool.COMMA));
        this.baseMapper.update(accidentAlertsFile,new LambdaQueryWrapper<AccidentAlertsFile>().in(AccidentAlertsFile::getAlertsFileId,list));
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-08-23 13:28:12
    * @param accidentAlertsFileIds accidentAlertsFileIds
    */
    @Override
    @Transactional
    public void physicallyDeleteAccidentAlertsFile(String accidentAlertsFileIds) {
        List<String> list = Arrays.asList(accidentAlertsFileIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentAlertsFile> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentAlertsFile::getAlertsFileId,list);
        this.remove(wapper);
    }

    @Override
    public void deleteByAlertIds(String alertsId) {
        List<String> list = Arrays.asList(alertsId.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentAlertsFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AccidentAlertsFile::getAlertsId,list);
        this.remove(wrapper);
    }
}
